﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using zichan.Models;

namespace zichan.Controllers
{
    public class IPsController : BaseController
    {
        // GET: IPs
        public ActionResult Index(string Ip, int? MachineRoomId,int? statu, int? page, int? pageSize, int? pageCount, int? selectedPage)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> pitems = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(pitems, "Value", "Text", ViewBag.pageIndex);

            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", MachineRoomId);
            ViewBag.MachineRoomId = MachineRoomId;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "未使用" }, new SelectListItem { Value = "1", Text = "已使用" } };
            ViewBag.status = new SelectList(items, "Value", "Text", statu);
            ViewBag.statu = statu;
            ViewBag.Ip = Ip;
            return View(IPService.List(Ip, MachineRoomId,statu, page.Value, pageSize.Value));
        }
        public ActionResult Create()
        {
            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(IP model, string identification)
        {
            if (Session["identify"] != null && Session["identify"].ToString() == identification)
            {
                TempData["alert"] = "sorry#" + "不能重复提交";
                return RedirectToAction("Index");
            }
            Session["identify"] = identification;
            if (!ModelState.IsValid)
            {
                ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name",model.MachineRoomId);
                return View(model);
            }
            string err = IPService.Add(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#添加成功";
                MachineRoomLogService.Add(2, "添加" + "\"" + model.Ip + "\"");
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        public ActionResult CreateList()
        {
            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult CreateList(ListIpModel model, string identification)
        {
            if (Session["identify"] != null && Session["identify"].ToString() == identification)
            {
                TempData["alert"] = "sorry#" + "不能重复提交";
                return RedirectToAction("Index");
            }
            Session["identify"] = identification;
            if (!ModelState.IsValid)
            {
                ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                return View(model);
            }
            List<string> list = model.nameList.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach(string s in list)
            {
                Regex reg = new Regex(@"^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))$");
                Match match = reg.Match(s);
                if (match.ToString() == "")
                {
                    ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                    ModelState.AddModelError("nameList", "请输入正确格式的数据");
                    return View(model);
                }
            }
            List<string> existIp;
            List<string> ipListForLog;
            string err = IPService.AddBatch(list, out existIp, out ipListForLog, model.MachineRoomId);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#添加成功";
                if (existIp.Count() > 0)
                {
                    TempData["alert"] = "error#" + string.Join("、", existIp) + "因重命名无法添加";
                }
                if (ipListForLog.Count() > 0)
                {
                    MachineRoomLogService.Add(2, "添加" + "\"" + string.Join("、", ipListForLog) + "\"");
                }
            }
            else
                TempData["alert"] = "error#添加失败";

            return RedirectToAction("Index");
        }
        public ActionResult Edit(int id)
        {
            IP model = IPService.Get(id);
            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
            if (model == null)
                return HttpNotFound();

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(IP model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                return View(model);
            }
            string oldIp = IPService.Get(model.Id).Ip;
            int oldClassId = IPService.Get(model.Id).MachineRoomId;
            string err = IPService.Edit(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#修改成功";
                if (oldIp != model.Ip)
                { MachineRoomLogService.Add(2, "修改" + "\"" + oldIp + "\"为\"" + model.Ip + "\""); }
                if (oldClassId != model.MachineRoomId)
                {
                    string oldClassName = MachineRoomService.Get(oldClassId).Name;
                    string className = MachineRoomService.Get(model.MachineRoomId).Name;
                    MachineRoomLogService.Add(2, "修改" + "\"" + model.Ip + "-" + "所属机房：" + oldClassName + "\"为\"" + model.Ip + "-" + "所属机房：" + className + "\"");
                }
            }
            else
                TempData["alert"] = "error#" + err;
            return RedirectToAction("index");
        }

        public ActionResult Delete(int id)
        {
            string err = IPService.Delete(id);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#删除成功";
            }
            else
                TempData["alert"] = "error#" + err;

            return RedirectToAction("index");
        }
    }
}